<!-- 搜索商品 -->
<template>
	<view>
		<view class="bg-Container header-height">
			<view class="header-bg"></view>

			<!-- 导航栏 -->
			<view class="header-Container header-height">
				<view class="content2">

					<image :src="icon.return" class="return" @click="gotoNavBack()"></image>

					<!-- 模糊搜索 -->
					<view class="search-input">
						<image :src="icon.search" mode="widthFix"></image>
						<input :focus="searching" key="listSearch" confirm-type="search" @confirm="confirmSearchHandler()"
						 placeholder-class="placeholder" :placeholder="searchHolder" v-model="search" @click="focusHandler()" />
					</view>

					<view v-if="searching" class="btn-icon" style="color: #FFFFFF;white-space: nowrap;" @click="searching = false;">取消</view>
					<view v-else @click="toggleMenuListHandler()" class="msgCenter-Container">
						<image class="menu" :src="icon.menuBtn_white"></image>
						<view v-if="noticeTipCount || $store.state.msgNumber" class="msgCount">
							{{noticeTipCount + $store.state.msgNumber > 99 ? '99+' : noticeTipCount + $store.state.msgNumber}}
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- <view v-if="showDetailMenu" @click="toggleMenuListHandler()" style="position: fixed;width: 100%;height: 100vh;top:0;z-index: 99999;">
			<g-navigation-menu></g-navigation-menu>
		</view> -->

		<!-- 搜索历史/发现 -->
		<view v-if="searching" class="search-block headerNext">
			<view class="search-title" v-if="searchHistory.length >0">
				<view class="">
					历史搜索
				</view>
				<image @click="deleteSearchHistory()" :src="icon.delete" class="search-img"></image>
			</view>
			<view class="search-message" v-if="searchHistory.length >0">
				<view @click="getSearchGoodsList(item.csSearchContent)" v-for="(item, index) of searchHistory" :key="item.csId">
					{{ item.csSearchContent }}
				</view>
			</view>
			<view class="search-title">搜索发现</view>
			<view class="search-message">
				<view @click="getSearchGoodsList(item.word)" v-for="(item, index) of searchFound" :key="item.id">
					{{ item.word }}
				</view>
			</view>
		</view>


		<!-- 商品列表 -->
		<block v-if="!searching">
			<!-- 筛选框 -->

			<view class="filterBar headerNext" style="width: 100%;">
				<navbar @getAction="tabClick"></navbar>
			</view>

			<view class="row-between" style="width: 100%;flex-wrap: wrap;padding: 0 30upx;" v-if="listType==0 && goodsList && goodsList.length">
				<home-goods style="width: calc(100% /2 - 11upx);height: calc();" v-for="item in goodsList" :item="item" :key="item.goodsCd" />
			</view>
			<view v-else-if="listType && goodsList && goodsList.length" style="width: 100%;padding: 0 30upx;">
				<goods-across v-for="item in goodsList" :item="item" :key="item.goodsCd" />
			</view>
			<g-empty v-else />

			<!-- <view class="good_list" v-if="listType==0">
				<goodList v-if="goodsList && goodsList.length" :list='goodsList'></goodList>
				<view v-else>
				    <image class="noData" src="/static/images/common/noData.png"></image>
				</view>
			</view>
			
			<view class="good_list2" v-else>
				<goodListtransverse v-if="goodsList && goodsList.length" :list='goodsList'></goodListtransverse>
				<view v-else>
				    <image class="noData" src="/static/images/common/noData.png"></image>
				</view>
			</view> -->
		</block>

		<!-- 详情菜单 -->
		<view v-if="showDetailMenu" @click="toggleMenuListHandler()" class="detailMenu-Container search-detailMenu">
			<detailMenu></detailMenu>
		</view>
		<view style="height: 60upx;"></view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';

	import goodList from '@/components/goodList/goodList';
	import goodListtransverse from '@/components/goodList/goodList-transverse';
	import navbar from '@/components/navbar/navbar';
	import detailMenu from "@/components/detailMenu/detailMenu";

	export default {
		components: {
			goodList,
			navbar,
			goodListtransverse,
			detailMenu
		},
		computed: {
			...mapState(['imgSite', 'hasLogin'])
		},
		data() {
			return {
				icon: {
					delete: '/static/images/list/delete.png',
					return: '/static/images/common/return-white.png',
					headerBg: '/static/images/common/header.png',
					search: '/static/images/index/search.png',
					menuBtn_white: "/static/images/common/msg-center-white.png",
				},

				noticeTipCount: 0,

				//详情菜单
				showDetailMenu: false,

				searching: false,
				search: '',
				listType: 0,
				sortType: 0,
				goodsList: [],

				searchHistory: [],
				searchFound: [],
				searchHolder: '请输入商品或品牌关键字',


				//分页相关
				pageNum: 1,
				pageSize: 10
			};
		},
		onLoad(options) {
			console.log("options", options)
			let that = this;
			uni.getStorage({
				key: 'type',
				success(res) {
					console.log(res.data)
					if (res.data == "searching") {
						that.searching = true;
						uni.removeStorage({
							key: "type"
						})
					}
				}
			});
			if (options.msg != '') {
				that.searchHolder = options.msg;
				that.search = options.msg;
			}
			this.pageNum = 1;
			this.searching = false;
			this.getSearchGoodsList(this.search);

			//搜索历史&热词
			this.getUserSearchHisgory();
			this.getUserSearchFound();
		},
		onPullDownRefresh() {
			this.pageNum = 1;
			this.goodsList = []
			this.getSearchGoodsList(this.search);
		},
		onReachBottom() {
			if (!this.goodsList.length) return;
			this.pageNum += 1;
			this.getSearchGoodsList(this.search);

		},
		onShow() {

			if (this.hasLogin) {

				let that = this;
				this.$api.globalUtil.requestAjax({
					url: '/customerNoticeApi/customerNoticeUnreadCountApi',
					method: 'POST',
					data: {
						cnType: "",
						cnStatus: '0'
					},
					success: function(res) {
						that.noticeTipCount = res.data.response;
					}
				});
			}

			this.showDetailMenu = false;
		},
		methods: {
			//商品列表
			async getSearchGoodsList(keyword) {
				var that = this;
				that.search = keyword;
				if (that.searchHolder != '请输入商品或品牌关键字' & that.search == '') {
					that.search = that.searchHolder;
				}
				this.searching = false;
				this.$api.showLoading()
				await this.$api.globalUtil.requestAjax({
					url: '/GoodsApi/commodityClassificationListApi',
					method: "POST",
					data: {
						isSearch: true,
						pageNum: this.pageNum, //页码
						pageSize: this.pageSize, //显示条数
						sort: this.sortType || '', //综合排序:null 销量排序:1 新品排序:2 价格ASC:3 价格DESC:4
						search: this.search
					},
					success: (res) => {
						if (!res.data.success) return this.$api.msg(res.data.msg || '获取失败');
						let arr = res.data.response.results || [];
						if (!arr.length && this.pageNum != 1) return this.$api.msg('没有更多了...');
						arr.forEach(item => {
							let priceArr = (item.salesPrice / 100).toFixed(2).split('.');
							item.salesPrice = priceArr[0]
							item.zero = priceArr[1] ? '.' + priceArr[1] : ''
							item.marketPrice = (item.marketPrice / 100).toFixed(2)
							item.goodsLabelArr = item.goodsLabel ? item.goodsLabel.split(',') : []
						})
						this.goodsList = this.goodsList.concat(arr)
					},
					complate: () => (uni.stopPullDownRefresh(),this.$api.hideLoading())
				});
			},
			//返回
			gotoNavBack() {
				uni.navigateBack();
			},

			//详情菜单
			toggleMenuListHandler() {
				this.showDetailMenu = !this.showDetailMenu;
			},

			//搜索处理
			confirmSearchHandler() {
				this.pageNum = 1;
				this.searching = false;
				this.getSearchGoodsList(this.search);
			},


			//列表筛选条件
			tabClick(params) {

				if (params.listFlag) {
					this.listType = params.listType;
				} else if (params.sortFlag) {

					if (params.sortType == 3) {
						this.sortType = params.priceSort;
					} else {
						this.sortType = params.sortType;
					}
					this.pageNum = 1;
					this.goodsList = []
					this.getSearchGoodsList(this.search);
				}
			},


			//搜索点击聚焦
			focusHandler() {
				this.searching = true;

				//搜索历史&热词
				this.getUserSearchHisgory();
				this.getUserSearchFound();

			},

			//搜索历史
			getUserSearchHisgory() {

				let that = this;
				this.$api.globalUtil.requestAjax({
					url: '/customerSearchApi/customerSearchListApi',
					method: 'POST',
					success: function(res) {
						that.searchHistory = res.data.response;
					}
				});
			},

			//搜索发现
			getUserSearchFound() {
				let that = this;
				this.$api.globalUtil.requestAjax({
					url: '/HotSearchApi/getHotSearchByCategoryForApi',
					method: 'POST',
					success: function(res) {
						that.searchFound = res.data.response;
					}
				});
			},

			//清空历史搜索记录
			deleteSearchHistory() {
				let that = this;
				uni.showModal({
					content: '确认删除搜索记录？',
					confirmText: '确认',
					cancelText: '取消',
					success(res) {
						if (res.confirm) {
							that.$api.globalUtil.requestAjax({
								url: '/customerSearchApi/customerSearchDeleteAllApi',
								method: 'POST',
								success: function(res) {
									that.searchHistory = [];
								}
							});
						}
					}
				})
			}
		}
	};
</script>

<style scoped lang="scss">
	.search-view {
		width: 520upx;
		height: 64upx;
		margin-left: 33upx;
		background: #FFFFFF;
		border: 1px solid #F56D0A;
		border-radius: 32upx;
		padding: 0 24upx;

		image {
			width: 26upx;
			height: 26upx;
			margin-right: 10upx;
		}

		.search-input {
			color: #999999;
			font-size: 26upx;
		}

		.placeholder {
			font-size: 26upx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #999999;
		}

	}

	.menu-icon {
		position: relative;
	}

	.uni-badge-left-margin {
		position: absolute;
		top: -12upx;
		right: -17upx;
	}



	/* header */
	.bg-Container {
		width: 100%;
		z-index: 10;

		.header-bg {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			background: #FE5E49;
		}

		.header-Container {
			width: 100%;
			height: 176rpx;

			.content2 {
				display: flex;
				align-items: center;
				justify-content: space-between;
				position: absolute;
				bottom: 0;
				padding: 0 46rpx 0 0;
				width: 100%;
				height: 88rpx;

				.return {
					width: 44rpx;
					height: 44rpx;
					margin-left: 19rpx;
				}

				.search-input {
					position: relative;
					margin-left: 40rpx;
					width: 540rpx;
					// height: 56rpx;
					border-radius: 28px;
					border: 1rpx solid rgba(245, 109, 10, 1);

					image {
						position: absolute;
						left: 30rpx;
						top: 16rpx;
						width: 26rpx;
						height: 26rpx;
					}

					input {
						box-sizing: border-box;
						padding-left: 74rpx;
						height: 56rpx;
						line-height: 28rpx;
						font-weight: 400;
						font-family: MicrosoftYaHei;
						font-size: 28rpx;
						border-radius: 28rpx;
						background-color: #ffffff;
					}

					.placeholder {
						height: 28rpx;
						line-height: 28rpx;
						font-weight: 400;
						font-family: MicrosoftYaHei;
						font-size: 28rpx;
						color: #999999;
					}
				}

				.btn-icon {
					width: 50rpx;
					height: 51rpx;
					margin-left: 35rpx;
					font-size: 25rpx;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
		}
	}

	.msgCenter-Container {
		display: flex;
		align-items: center;
		margin-left: 34rpx;
		height: 44rpx;

		.menu {
			width: 40rpx;
			height: 40rpx;
		}

		.msgCount {
			position: absolute;
			bottom: 50rpx;
			right: 32rpx;
			min-width: 26rpx;
			width: auto;
			height: 26rpx;
			line-height: 26rpx;
			text-align: center;
			font-size: 20rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #FE5E49;
			background: rgba(255, 255, 255, 1);
			box-shadow: 0px 1rpx 4rpx 0px rgba(68, 68, 68, 0.2);
			border-radius: 26rpx;
		}
	}

	// 商品列表
	/deep/.list-pages {
		padding: 0 30rpx;

		.list-block {
			margin-bottom: 30rpx;
			width: 336rpx;
			box-shadow: 0 2rpx 10rpx 0 rgba(103, 103, 103, 0.1);
			border-radius: 10rpx;

			.list-poster {
				width: 336rpx;
				height: 336rpx;
			}
		}
	}

	.good_list2 {
		width: 100%;
		background-color: white;
	}

	// 搜索框
	.search-block {
		top: 176rpx;
		z-index: 1;
		width: 100%;
		height: 100vh;
		background-color: white;
		border-radius: 20rpx 20rpx 0px 0px;

		.search-title {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin: 0 0 0 29rpx;
			height: 95rpx;
			line-height: 95rpx;
			font-size: 28rpx;
			font-weight: bold;

			.search-img {
				position: absolute;
				height: 31rpx;
				width: 31rpx;
				right: 30rpx;
			}
		}

		.search-message {
			display: flex;
			width: 718rpx;
			flex-wrap: wrap;
			line-height: 63rpx;
			margin-left: 29rpx;

			view {
				flex-shrink: 0;
				color: #666;
				padding: 0 22rpx 0 22rpx;
				background-color: #eeeeee;
				border-radius: 27rpx;
				font-size: 23rpx;
				height: 53rpx;
				margin-right: 18rpx;
				margin-bottom: 18rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}

			view:last-child {
				margin-right: 0;
			}
		}
	}
</style>
